package com.lc.user.express.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lc.user.express.R;
import com.lc.user.express.app.AppContext;
import com.lc.user.express.httpserver.GetAddrDelete;
import com.lc.user.express.model.UsedRouteModel;
import com.lc.user.express.ordering.ManageUsedRouteActivity;
import com.lc.user.express.ordering.SelectUsedRouteActivity;
import com.lc.user.express.utils.GetToast;
import com.lc.user.express.view.dialog.AffirmDialog;
import com.zcx.helper.http.AsyCallBack;

import java.util.List;

/**
 * Created by Administrator on 2016/3/7.
 */
public class ManageUsedRouteAdapter extends BaseListAdapter<UsedRouteModel> {
    final int TYPE_1 = 0;
    final int TYPE_2 = 1;

    public ManageUsedRouteAdapter(Context context, List<UsedRouteModel> data) {
        super(context, data);
    }

    @Override
    public int getItemViewType(int position) {
        if (data.get(position).isEnd()) {
            return TYPE_1;
        } else {
            return TYPE_2;
        }
    }

    @Override
    public int getViewTypeCount() {
        // TODO Auto-generated method stub
        return 2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        ViewHolder1 viewHolder1 = null;
        int type = getItemViewType(position);
        UsedRouteModel model = data.get(position);
      if (convertView == null) {
            if (type == TYPE_1) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.footer_select_used_addr_list, null);
                AppContext.ScaleScreenHelper.loadView((ViewGroup) convertView);
                viewHolder1 = new ViewHolder1();
                viewHolder1.tv_title = (TextView) convertView.findViewById(R.id.tv_dic);
                convertView.setTag(viewHolder1);
            } else {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_mange_used_route_list, null);
                AppContext.ScaleScreenHelper.loadView((ViewGroup) convertView);
                viewHolder = new ViewHolder();
                viewHolder.tv_start_addr = (TextView) convertView.findViewById(R.id.tv_start_addr);
                viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
                viewHolder.tv_end_addr_count = (TextView) convertView.findViewById(R.id.tv_end_addr_count);
                viewHolder.iv_delete = (ImageView) convertView.findViewById(R.id.iv_delete);
                convertView.setTag(viewHolder);
            }
        }
        setContentView(convertView, model, type, position);
        return convertView;
    }

    private void setContentView(View contentView, final UsedRouteModel model, int type, final int position) {
        if (type == TYPE_1) {
            ViewHolder1 viewHolder1 = (ViewHolder1) contentView.getTag();
            viewHolder1.tv_title.setText(model.getName());
        } else {
            ViewHolder viewHolder = (ViewHolder) contentView.getTag();
            viewHolder.tv_name.setText(model.getName());
            viewHolder.tv_end_addr_count.setText(model.getEndAddrCount()-1 + "个目的地");
            viewHolder.tv_start_addr.setText("始发地：" + model.getStartAddr());
            viewHolder.iv_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new AffirmDialog(mContext, "要删除这条常用路线吗？", "保留", "删除", R.style.MyDialog) {

                        @Override
                        protected void onYesClick() {

                            // TODO Auto-generated method stub
                            deleteAddr(model.getRouteId());

                        }
                    }.show();
                }
            });
        }
    }

    private void deleteAddr(String routeId) {
        new GetAddrDelete(AppContext.myPreferences.getUid(), routeId, new AsyCallBack<Object>() {
            @Override
            public void onSuccess(String toast, int type, Object o) throws Exception {
                if("1".equals(o)){
                    if (SelectUsedRouteActivity.onRefreshView != null) {
                        SelectUsedRouteActivity.onRefreshView.onRefresh();
                    }
                    if(ManageUsedRouteActivity.onRefreshView != null){
                        ManageUsedRouteActivity.onRefreshView.onRefresh();
                    }
                }
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                if(!TextUtils.isEmpty(toast)){
                    GetToast.useString(mContext, toast);
                }
            }
        }).execute(mContext);
    }

    private class ViewHolder {
        TextView tv_start_addr;
        TextView tv_name;
        TextView tv_end_addr_count;
        ImageView iv_delete;
    }

    final static class ViewHolder1 {
        TextView tv_title;
    }
}
